﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace WpfView
{
    /// <summary>
    /// Interaction logic for WindowMain.xaml
    /// </summary>
    public partial class WindowMain : Window
    {
        public WindowMain()
        {
            InitializeComponent();
        }

        private void btnGo_Click(object sender, RoutedEventArgs e)
        {
            String fileName = SelectOneFile();
            if (!String.IsNullOrEmpty(fileName))
            {
                String fileString = ReadFileString(fileName);
                txtShow.Text = fileString;
            }
        }

        private void btnConvert_Click(object sender, RoutedEventArgs e)
        {
            Byte[] data = System.Text.Encoding.Default.GetBytes(txtShow.Text);
            txtShow.Text = System.Text.Encoding.GetEncoding("gb2312").GetString(data);
        }

        private void lblBatch_Click(object sender, RoutedEventArgs e)
        {
            String[] fileNames = SelectMultiFiles();
            if (fileNames != null) {
                foreach (String fileName in fileNames) {
                    String fileString = ReadFileString(fileName);
                    WriteFile(fileName, fileString);
                }
            }
            MessageBox.Show("Done!");
        }

        public static String SelectOneFile()
        {
            // Create OpenFileDialog
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            // Set filter for file extension and default file extension
            dlg.DefaultExt = ".txt";
            dlg.Filter = "Text documents (*.txt)|*.txt|All files (*.*)|*.*";

            // Display OpenFileDialog by calling ShowDialog method
            Nullable<Boolean> result = dlg.ShowDialog();

            if (result == true)
            {
                return dlg.FileName;
            }
            return String.Empty;
        }

        public static String[] SelectMultiFiles()
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            dlg.DefaultExt = ".txt";
            dlg.Filter = "Text documents (*.txt)|*.txt|All files (*.*)|*.*";
            dlg.Multiselect = true;

            Nullable<Boolean> result = dlg.ShowDialog();

            if (result == true)
            {
                return dlg.FileNames;
            }
            return null;
        }

        public static String ReadFileString(String fileName)
        {
            try
            {
                FileStream fs = File.OpenRead(fileName);
                Byte[] data = new Byte[fs.Length];
                fs.Read(data, 0, data.Length);
                fs.Close();

                //return System.Text.UTF8Encoding.UTF8.GetString(data);
                return System.Text.Encoding.GetEncoding("gb2312").GetString(data);
            }
            catch (FileNotFoundException)
            {
                throw new Exception("File not found.");
            }
        }

        public static void WriteFile(String fileName, String fileContent)
        {
            FileAttributes fa = FileAttributes.Normal;

            if (File.Exists(fileName))
            {
                FileInfo fiOriginal = new FileInfo(fileName);
                if (fiOriginal.Attributes.ToString().IndexOf("ReadOnly") != -1)
                {
                    fa = fiOriginal.Attributes;
                    fiOriginal.Attributes = FileAttributes.Normal;
                }
            }

            using (FileStream output = new FileStream(fileName, FileMode.Create))
            {
                Byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(fileContent);
                output.Write(data, 0, data.Length);
                output.Close();
            }

            FileInfo fiFinal = new FileInfo(fileName);
            fiFinal.Attributes = fa;
        }
    }
}
